import random

import requests
from lxml import html

from news_2.src.main.utils.file_tool import read_file_contents


class HtmlTagPath:
    def __init__(self, key, x_path, attr):
        self.key = key
        self.path = x_path
        self.attr = attr
        self.element = None
        self.data_value = None

    """
        是否为提取属性数据值
    """

    def __is_attr(self):
        if self.attr is not None and self.attr != "":
            return True
        else:

            return False

    """
        设置标签元素
    """

    def set_element(self, html_content):

        if self.path is not None or not self.path != "":
            tree = html.fromstring(html_content)
            element = tree.xpath(self.path)
            self.element = element

    """
        根据标签元素提取数据
    """

    def process_data_value(self):
        if self.element is None or len(self.element) < 1:
            return
        if self.__is_attr():
            self.data_value = self.element[0].get(self.attr)
        else:
            self.data_value = self.element[0].text_content()

    """
        返回数据
    """

    def get_data_value(self):
        return self.data_value


class LxmlExtractNews:
    def __init__(self, url='', encode='', decode='utf-8'):
        self.url = url
        self.encode = encode
        self.decode = decode

        self.html_content = ""
        self.tags = []
        self.data = []

    # 是否需要解码
    def __is_decode(self):
        if self.encode is not None and self.encode != "":
            return True
        else:
            return False

    def add_tags(self, tag_obj):
        self.tags.append(tag_obj)

    """
        发起请求获取 html文本
    """

    def request_html_content(self):
        proxies = {
            'http': "http://127.0.0.1:8889",
            'https': "http://127.0.0.1:8889",
        }

        user_agent_list = [
            'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
            'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
            'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
            'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
            'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
            'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
            'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
            "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Mobile Safari/537.36",
        ]
        headers = {
            "User-Agent": random.choice(user_agent_list),

        }

        try:
            response = requests.get(self.url, headers=headers, proxies=proxies)

            # 检查响应状态码
            if 200 <= response.status_code < 300:
                print('请求成功:', response.status_code)
                self.html_content = response.text
            else:
                print('请求失败:', response.status_code)

        except requests.exceptions.RequestException as e:
            print('请求异常:', e)

    def reading_html_content(self, path):
        self.html_content = read_file_contents(path)

    """
        解码 html文本
    """

    def decode_html(self):
        if self.html_content is not None and self.html_content != "" and self.__is_decode():
            self.html_content = self.html_content.encode(self.encode).decode(self.decode)
            print("解码成功")
        print("无需解码")

    """
        转换path数据为对象
    """

    def build_html_tags(self, tag_path=None):
        print("构建加工对象")
        if tag_path is None or not isinstance(tag_path, list):
            tag_path = []
        for tag in tag_path:
            tag_obj = HtmlTagPath(key=tag['key'], x_path=tag['x_path'], attr=tag['is_attr'])
            self.add_tags(tag_obj)

    """
        使用HtmlTag对象获取数据
    """

    def process_html_tag_data(self):
        print("调用加工对象加工数据")
        for tag in self.tags:
            # 1.配置标签
            tag.set_element(self.html_content)
            # 2.提取数据
            tag.process_data_value()
